package com.blex.misp.action;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.blex.misp.entity.Product;
import com.blex.misp.service.ProductService;
import com.blex.misp.utils.ExceptionUtil;
import com.blex.misp.action.Json;
import com.opensymphony.xwork2.ModelDriven;

@Controller
public class ProductAction extends BaseAction implements ModelDriven<Product>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger.getLogger(ProductAction.class);
	
	@Autowired
	private ProductService productService;
	public Product product = new Product();

	/**
	 * 得到数据列表
	 */
	@Action("productGrid")
	public void grid(){
		int total = productService.getTotal();
		List<Product> list = productService.findAll(page, rows, sort, order);		
		writeJson(total,list);
	}
	/**
	 * 添加操作
	 */
	@Action("productAdd")
	public void add(){
			Json result = new Json();
			try {
				productService.save(product);
				result.setSuccess(true);
				result.setMsg("添加产品成功");					
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("添加产品失败");	
				System.out.println(ExceptionUtil.getExceptionMessage(e));
			}
			System.out.println(product.getId());
			System.out.println(product.getProductno());
			System.out.println(product.getProductname());
			System.out.println(product.getProducttype());
			System.out.println(product.getDescription());
			System.out.println(product.getPrice());
			writeJson(result);
	}
	/**
	 * 修改操作
	 */
	@Action("productEdit")
	public void edit() {
			Json result = new Json();
			try {
				productService.update(product);
				result.setSuccess(true);
				result.setMsg("修改产品成功");
				result.setSuccess(true);
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("修改产品失败");
			}
			writeJson(result);
	}
	/**
	 *  删除操作
	 */
	@Action("productRemove")
	public void remove() {
			Json result = new Json();
			try {
				productService.delete(product.getId());
				result.setSuccess(true);
				result.setMsg("删除产品成功");
			} catch (Exception e) {
				logger.debug(ExceptionUtil.getExceptionMessage(e));
				result.setMsg("删除产品失败");
			}
			writeJson(result);
	}
	
	@Override
	public Product getModel() {
		return product;
	}

}
